﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Baddies.Utils;
using BaddiesEditor.Events;

namespace BaddiesEditor.CustomComponent
{
    /// <summary>
    /// Form to select the size modification options.
    /// </summary>
    public partial class EditMapSize : Form
    {
        /// <summary>
        /// Handler for when the form finishes correctlly.
        /// </summary>
        private EventHandler acceptButtonHandler;

        /// <summary>
        /// Initializes a new instance of the EditMapSize class.
        /// </summary>
        /// <param name="acceptHandler">Handler for the ok button event.</param>
        public EditMapSize(EventHandler acceptHandler)
        {
            this.InitializeComponent();
            this.acceptButtonHandler += acceptHandler;
        }

        /// <summary>
        /// Handler for the Ok button.
        /// </summary>
        /// <param name="sender">Class that sends the message.</param>
        /// <param name="e">Arguments of the call.</param>
        private void AcceptButton_Click(object sender, EventArgs e)
        {
            // Arguments for the message to the editor form.
            EditMapSizeArg args = new EditMapSizeArg();

            if (this.DirectionComboBox.SelectedIndex == -1)
            {
                this.ErrorForm("Select a side of the map to add the lines!");
                return;
            }
            else 
            {
                switch (this.DirectionComboBox.SelectedIndex)
                {
                    case 0: 
                        args.Side = Util.Dir.Left; 
                        break;
                    case 1: 
                        args.Side = Util.Dir.Right; 
                        break;
                    case 2: 
                        args.Side = Util.Dir.Up; 
                        break;
                    case 3: 
                        args.Side = Util.Dir.Down; 
                        break;
                }
            }

            if (!this.ConfirmTextBoxValues(this.NumberLinesTextBox))
            {
                this.ErrorForm("Input a valid value as the number of lines to add!");
                return;
            }
            else
            {
                args.NumberLines = this.StringToInt(this.NumberLinesTextBox.Text);
            }

            this.acceptButtonHandler(this, args);
            this.Close();
        }

        /// <summary>
        /// Creates a new error form with the specified error.
        /// </summary>
        /// <param name="msg">Error to display.</param>
        private void ErrorForm(string msg)
        {
            ErrorForm error = new ErrorForm();
            error.SetErrorMsg(msg);
            error.Show();
        }

        /// <summary>
        /// Confirms if a textbox adjust to the specified requirements.
        /// </summary>
        /// <param name="box">Textbox to check.</param>
        /// <returns>True if it does, false otherwise.</returns>
        private bool ConfirmTextBoxValues(TextBox box)
        {
            string text = box.Text;

            // Check it's a number
            int num = 0;
            if (!int.TryParse(text, out num))
            {
                return false;
            }

            // Check valid range
            if (num < -999 || num > 999)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Transforms a string that has been confirmed represents a number
        /// into a number.
        /// </summary>
        /// <param name="text">Text to translate.</param>
        /// <returns>Number in the text.</returns>
        private int StringToInt(string text)
        {
            int num = 0;
            int.TryParse(text, out num);
            return num;
        }

        /// <summary>
        /// Called when the user clicks the cancel button.
        /// </summary>
        /// <param name="sender">Class that sends the message.</param>
        /// <param name="e">Arguments of the call.</param>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
